package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.system.domain.vo.ExtractInfo;
import com.ruoyi.system.domain.vo.KeywordInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.net.ConnectException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class NlpcUtil {
    @Value("${nlpc.host}")
    private String host;

    private String GET_PERSON_NAME_URL = "get_personname/";

    private String GET_LOCATION_NAME_URL = "get_locationname/";

    private String GET_ORGANIZATION_NAME_URL = "get_organizationname/";

    private String GET_KEYWORD_URL = "get_keyword/";

    private String GET_SUMMARY_URL = "get_summary/";

    private String GET_EXTRACT_INFO_URL = "get_extract_info/";

    private String GET_PRECISE_EMOTION_URL = "get_preciseemotion/";

    public String getSummary(String text){
        LinkedMultiValueMap paras = new LinkedMultiValueMap();
        paras.add("text", text);

        String summary = requestNlpc(GET_SUMMARY_URL, paras);
        return summary;
    }

    public ExtractInfo getExtractInfo(String text){
        LinkedMultiValueMap paras = new LinkedMultiValueMap();
        paras.add("text", text);

        String result = requestNlpc(GET_EXTRACT_INFO_URL, paras);
        ExtractInfo extractInfo = JSONObject.parseObject(result, ExtractInfo.class);
        return extractInfo;
    }

    public String getPreciseEmotion(String text, String originEmotion){
        LinkedMultiValueMap paras = new LinkedMultiValueMap();
        paras.add("text", text);
        paras.add("origin_emotion", originEmotion);

        String preciseEmotion = requestNlpc(GET_PRECISE_EMOTION_URL, paras);
        return preciseEmotion;
    }

    public List<KeywordInfo> getHotWords(String text, int wordNum){
        LinkedMultiValueMap paras = new LinkedMultiValueMap();
        paras.add("text", text);
        paras.add("wordNum", wordNum);

        String result = requestNlpc(GET_KEYWORD_URL, paras);
        List<KeywordInfo> hotwords = JSONObject.parseArray(result, KeywordInfo.class);;
        return hotwords;
    }

    public List<String> getPersons(String text, int wordNum){
        LinkedMultiValueMap paras = new LinkedMultiValueMap();
        paras.add("text", text);
        paras.add("wordNum", wordNum);

        String result = requestNlpc(GET_PERSON_NAME_URL, paras);
        List<String> list=new ArrayList<>();
        if(StringUtil.isNotEmpty(result)){
            list = Arrays.asList(result.split(","));
        }
        return list;
    }

    public List<String> getLocations(String text, int wordNum){
        LinkedMultiValueMap paras = new LinkedMultiValueMap();
        paras.add("text", text);
        paras.add("wordNum", wordNum);

        String result = requestNlpc(GET_LOCATION_NAME_URL, paras);
        List<String> list=new ArrayList<>();
        if(StringUtil.isNotEmpty(result)){
            list = Arrays.asList(result.split(","));
        }
        return list;
    }

    public List<String> getOrganizations(String text, int wordNum){
        LinkedMultiValueMap paras = new LinkedMultiValueMap();
        paras.add("text", text);
        paras.add("wordNum", wordNum);

        String result = requestNlpc(GET_ORGANIZATION_NAME_URL, paras);
        List<String> list=new ArrayList<>();
        if(StringUtil.isNotEmpty(result)){
            list = Arrays.asList(result.split(","));
        }
        return list;
    }


    private String requestNlpc(String url, LinkedMultiValueMap paras){
        url = host + url;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();

        factory.setConnectTimeout(30 * 1000);
        factory.setReadTimeout(30 * 1000);

        RestTemplate restTemplate = new RestTemplate(factory);
        HttpEntity requestBody = new HttpEntity(paras, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestBody, String.class);
        String result = responseEntity.getBody();

        return result;
    }




}
